package com.eric.demo.codestatics;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author ZhangGuangyin on 2018/8/28
 */
public class CodeStatics {

    public static void main(String[] argvs) throws IOException{
        File file = new File("D:\\opensource_code\\spring-framework");
        File[] listFiles = file.listFiles();
        for(File f : listFiles){
            int count = statics(f);
            System.out.println(f.getName() + " lines:" + count);
        }

    }

    public static int statics(File file) throws IOException {
        int count = 0;
        if(file.isDirectory()){
            File[] listFiles = file.listFiles();
            for(File f : listFiles){
                if(f.isDirectory()){
                    count += statics(f);
                }else if (f.getName().endsWith(".java")){
                    List<String> list = Files.readLines(f, Charsets.UTF_8);
                    for(String str : list){
                        if(StringUtils.isNotBlank(str)){
                            str = str.trim();
                            if(!(str.startsWith("//") || str.startsWith("/**") || str.equals("{") || str.equals("}"))){
                                count ++;
                            }
                        }
                    }
                }
            }
        }
        return count;
    }
}
